﻿using System;
using System.IO;
using System.Windows.Markup;
using System.Collections.Generic;
using Petra.Gis;
using Petra.Gis.Geometry.Support;
using Petra.Gis.RenderingServices.PlanarBase;
using Petra.Gis.RenderingServices.GDIPlus;
using Petra.Gis.ReferenceSystemServices;
using Petra.Gis.DataServices.SHP;
using Petra.Gis.DataServices.GDO.Sqlserver;


namespace testGridMap
{
  public class testGdoVectorData
  {

    public static void testMe()
    {
      BitmapMap map = new BitmapMap()
      {
        Width = 800,
        Height = 600,
        ProjectedCoordinateSystem = (ProjectedCoordinateSystem)Petra.Gis.Environment.SearchSrsBy("INGR", "49", "metre"),
        ProjectionExtent = new BoundingBox(866000, 1103000, 867000, 1104000),
        NominalScale = 5000,
        ApplyMapRangeAsSpatialFilter = true,
        Scale = 30000
      };


      // prepare some data...
      // prepare and open connection to spatial database
      // mssql server
      GdoSqlConnection c = new GdoSqlConnection()
      {
        ConnectionString =
        @"Data Source=localhost; 
         Initial Catalog=DS_LHPO_WORK;
				 Integrated Security = True;
         MultipleActiveResultSets=True"
      };

      c.Open();

      // prepare command
      GdoSqlCommand cmd = new GdoSqlCommand();
      cmd.Connection = c;
      cmd.CommandType = System.Data.CommandType.Text;
      cmd.CommandText = "select * from [DS_LHPO_WORK].[dbo].[F_A_PSK]";



      //prepare style
      //Petra.Gis.RenderingServices.GDIPlus.Style stl = new Petra.Gis.RenderingServices.GDIPlus.Style();
      //stl.StyleComponents.Add("Color", System.Drawing.Color.Black);
      //stl.StyleComponents.Add("LineWidth", 2);
      //stl.StyleComponents.Add("FillColor", System.Drawing.Color.FromArgb(128,50, 255, 50));

      // prepare renderer
      SurfaceRenderer g = new SurfaceRenderer()
      {
        Style = new Petra.Gis.RenderingServices.GDIPlus.Style()
        {
          Enabled = true,
          StyleScaleType = StyleScaleTypeEnum.ScaleIndependent,
          StyleComponents = new Petra.Gis.RenderingServices.PlanarBase.StyleComponents()
          {
            {"Color", System.Drawing.Color.Black},
            {"LineWidth", 2},
            {"FillColor", System.Drawing.Color.FromArgb(128, 50, 50, 250)}
          }
        }
      };

      //SurfaceRenderer g = new SurfaceRenderer()
      //{
      //  Map = map,
      //  Style = stl,
      //  SpatialFieldName = "Geometry" //optional, if user want to limit renderer to only one field
      //};

      Petra.Gis.RenderingServices.PlanarBase.LegendNodeBase l = map.Legend.CreateLegendNode(cmd, g);

      map.Legend.ChildNodes.Add(l);

      // prepare command
      cmd = new GdoSqlCommand();
      cmd.Connection = c;
      cmd.CommandType = System.Data.CommandType.Text;
      cmd.CommandText = "select * from [DS_LHPO_WORK].[dbo].[F_T_Porostni_skupina]";

      //prepare style
      Style stl = new Petra.Gis.RenderingServices.GDIPlus.Style()
      {
        Enabled = true,
        StyleScaleType = StyleScaleTypeEnum.ScaleIndependent
      };

      stl.StyleComponents.Add("FontFamilyName", "Arial");
      stl.StyleComponents.Add("FontSize", 10);
      stl.StyleComponents.Add("FontStyle", System.Drawing.FontStyle.Regular);
      stl.StyleComponents.Add("GraphicsUnit", System.Drawing.GraphicsUnit.Point);
      stl.StyleComponents.Add("FontColor", System.Drawing.Color.Black);

      // prepare another renderer
      LabelRenderer lr = new LabelRenderer() 
      {
        Style = stl
      };

      //SurfaceRenderer g = new SurfaceRenderer()
      //{
      //  Map = map,
      //  Style = stl,
      //  SpatialFieldName = "Geometry" //optional, if user want to limit renderer to only one field
      //};

      l = map.Legend.CreateLegendNode(cmd, lr);

      map.Legend.ChildNodes.Add(l);

      // draw map to output bitmap
      System.Drawing.Bitmap b = map.Draw();

      b.Save("c:\\temp\\druha.png", System.Drawing.Imaging.ImageFormat.Png);

      // test save to xml
      //string outStr = XamlWriter.Save(map);
    }

  }
}
